﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dapper.Core
{
    /// <summary>
    /// Class ConnectionProvider.
    /// </summary>
    public static class ConnectionProvider
    {
        /// <summary>
        /// Gets the type of the _connection.
        /// </summary>
        /// <value>The type of the _connection.</value>
        private static readonly string _connectionType
        {
            get
            {
                try
                {
                    return ConfigurationManager.AppSettings["DbType"];
                }
                catch
                {
                    return "SQLSERVER";
                }

            }
        }

        /// <summary>
        /// Gets the _connection string.
        /// </summary>
        /// <value>The _connection string.</value>
        private static readonly string _connectionString
        {
            get
            {
                try
                {
                    return ConfigurationManager.ConnectionStrings["Database"].ConnectionString;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
        /// <summary>
        /// Gets the _DB connection.
        /// </summary>
        /// <value>The _DB connection.</value>
        public static IDbConnection _dbConnection
        {
            //待优化，需要构建数据库连接池，此处仅示意，需要后续补充
            get
            {
                IDbConnection conn = null;
                switch (_connectionType)
                {
                    case "SQLSERVER":
                        conn = new SqlConnection(_connectionString);
                        break;
                    case "MYSQL":
                        conn = new MySqlConnection(_connectionString);
                        break;
                    default:
                        conn = new SqlConnection(_connectionString);
                        break;
                }
                conn.Open();
                return conn;
            }
        }
    }
}
